package com.youmans.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youmans.eduservice.entity.EduChapter;
import com.youmans.eduservice.entity.EduVideo;
import com.youmans.eduservice.entity.chapter.ChapterVo;
import com.youmans.eduservice.entity.chapter.VideoVo;
import com.youmans.eduservice.mapper.EduChapterMapper;
import com.youmans.eduservice.service.EduChapterService;
import com.youmans.eduservice.service.EduVideoService;
import com.youmans.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Youmans
 * @since 2022-05-07
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService ;//注入小结service

    //    课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduChapter> entities = baseMapper.selectList(wrapper);

        //章节数据封装
        List<ChapterVo> listChapter = new ArrayList<>();
        for (EduChapter entity : entities) {
            ChapterVo s = new ChapterVo();
            BeanUtils.copyProperties(entity, s);
            listChapter.add(s);
        }


        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduVideo> entitiesVideo = videoService.list(wrapperVideo);

        //小结数据封装
        List<VideoVo> videoList = new ArrayList<>();
        for (EduVideo entity : entitiesVideo) {
            VideoVo s = new VideoVo();
            BeanUtils.copyProperties(entity, s);
            videoList.add(s);
        }

        for (ChapterVo chapterVo : listChapter) {
            List<VideoVo> children  = new ArrayList<>() ;
            for (VideoVo videoVo : videoList) {
                if(Objects.equals(chapterVo.getId(),videoVo.getChapterId())){
                    children.add(videoVo) ;
                    chapterVo.setChildren(children);
                }
            }
        }
        return listChapter ;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
//        List<EduVideo> entitiesVideo = videoService.list(wrapper);
        //查询匹配条件
        int count = videoService.count(wrapper);
        if(count > 0){//查询出小结，不进行删除
            throw new GuliException(20001,"不能删除");
        }else{
            int i = baseMapper.deleteById(chapterId);
            return i>0 ;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>() ;
        wrapper.eq("course_id",courseId) ;
        baseMapper.delete(wrapper);
    }
}
